package com.bacga.abctest;


import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

@RestController
@Slf4j
public class MainController {


    @Autowired
    private ObjectFactory<HttpScheduler> factory;

    private AtomicInteger touchCount = new AtomicInteger();

    @RequestMapping("/touch")
    public String newDooo() {
        log.info(touchCount.incrementAndGet() + "");
        return "ok";
    }

    @RequestMapping("/clear")
    public String newDoooo() {
        touchCount.set(0);
        return "ok";
    }


    @RequestMapping("/test/thread/{thr}/request/{req}")
    public Map<String, Object> newDoo(@PathVariable("thr") int thr, @PathVariable("req") int req, @RequestParam String url) {
        Map param = new HashMap();
        WxGoodsVo vo = new WxGoodsVo();
        vo.setGoods_id("66");
        vo.setNum(1);
        List<WxGoodsVo> listv = new ArrayList<>();
        listv.add(vo);
        HttpScheduler scheduler = factory.getObject();
        List<Long> result = scheduler.doTest(url, listv, thr, req);

        Map<String,Object> data = new HashMap<>();
        Map<String, Object> zheXianMap = new HashMap<>();
        List<Integer> list = new LinkedList<>();
        for (int i = 1; i <= req; i++) {
            list.add(i);
        }
        zheXianMap.put("categories", list);
        zheXianMap.put("data", result);
        zheXianMap.put("counttime", scheduler.getFinishAllUseTime());
        zheXianMap.put("successrate", scheduler.getSuccessRate());
        data.put("zhexian",zheXianMap);
/*
        Map<String, Object> zhuZhuangMap = new HashMap<>();
        ArrayList<Long> sortResult = new ArrayList<>();
        sortResult.addAll(result);
        for (int i = 0; i < sortResult.size(); i++){
            for (int j = 0; j < sortResult.size() - 1 - i; j++){
                if(sortResult.get(j + 1) < sortResult.get(j)){
                    Long temp = sortResult.get(j + 1);
                    sortResult.set(j + 1,sortResult.get(j));
                    sortResult.set(j,temp);
                }
            }
        }

        int max = sortResult.get(sortResult.size() - 1).intValue();
        int min = sortResult.get(0).intValue();
        int content = (max - min) / 10;
        int ends = (max - min) % 10;
        List<String> listX = new ArrayList<>();
        List<Integer> listY = new ArrayList<>();
        if(content == 0){
            for (int i = 0; i < 10; i++){
                if(min + i + 1 <= max)
                    listX.add((min + i) + "~" + (min + i + 1));
                    listY.add(sortResult.lastIndexOf(Long.valueOf(min + i + 1)) - sortResult.indexOf(Long.valueOf(min + i)));
            }
        }else{
            for (int i = 0; i < 10; i++){

                if(i != 9){
                    listX.add((min + i * content) + "~" + (min + (i + 1) * content));
                    listY.add(sortResult.lastIndexOf(Long.valueOf(min + (i + 1) * content)) - sortResult.indexOf(Long.valueOf((min + i * content))));
                }else {
                    listX.add((min + i * content) + "~" + (min + (i + 1) * content + ends));
                    listY.add(sortResult.lastIndexOf(Long.valueOf((min + (i + 1) * content + ends))) - sortResult.indexOf(Long.valueOf((min + i * content))));
                }
            }
        }



        zhuZhuangMap.put("x",listX);
        zhuZhuangMap.put("data",listY);




        data.put("zhuzhuang",zhuZhuangMap);
        */
        return data;
    }

}
